package com.itbour.web.action.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.authority.RoleDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.authority.UserRoleInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author LQ 2017 角色用户信息
 *
 */
public class GetUserRoleInfoAction extends BaseAction {

	/** 全局 常用变量
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Object> mapResult=new HashMap<String, Object>();
	private List<UserInfoDto> listResult=new ArrayList<UserInfoDto>();
	private UserRoleInfoService userRoleInfoService;
	private List<RoleDto> listRole=new ArrayList<RoleDto>();
	private String findAcc;
	private String findName;
	private String findMobile;
	
	/**	分页获取用户信息
	 * 	return mapResult;
	 */
	public String pageUserRoleInfo(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer pageNo = Integer.parseInt(request.getParameter("page"));
			Integer pageSize =Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> parm=new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(findAcc)){
				parm.put("user_name", findAcc);
			}
			if(StringUtils.isNotEmpty(findName)){
				parm.put("nickname", findName);
			}
			if(StringUtils.isNotEmpty(findMobile)){
				parm.put("mobile", findMobile);
			}
			Page page=userRoleInfoService.getUserRoleInfo(parm, pageNo, pageSize);
			mapResult.put("total", page.getTotalCount());
			mapResult.put("rows",page.getResult());
		}
		return SUCCESS;
	}
	
	/**	列表显示角色分类
	 * 	return listRole;
	 */
	public String getUserRole(){
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		HttpServletRequest request=ServletActionContext.getRequest();
		if(user!=null && user.getUser_id()!=null){
			Integer user_id = Integer.parseInt(request.getParameter("user_id"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("user_id", user_id);
			listRole=userRoleInfoService.getUserRole(parm);
		}
		return SUCCESS;
	}

	/**	给用户授权
	 */
	public void updUserRole() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer userId = Integer.parseInt(request.getParameter("userId"));
			String checkId =request.getParameter("checkId");
			if(userId!=null){
				Map<String, Object> parm = new HashMap<String, Object>();
				parm.put("userId", userId);
				parm.put("checkId",checkId);
				parm.put("adminId",user.getUser_id());
				try{
					userRoleInfoService.updUserRole(parm);
					this.writeJSON("ok");
				}catch(Exception e){
					this.writeJSON("no");
				}
			}
		}else{
			this.writeJSON("error");
		}
	}
	

	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public List<UserInfoDto> getListResult() {
		return listResult;
	}
	public void setListResult(List<UserInfoDto> listResult) {
		this.listResult = listResult;
	}
	public UserRoleInfoService getUserRoleInfoService() {
		return userRoleInfoService;
	}
	public void setUserRoleInfoService(UserRoleInfoService userRoleInfoService) {
		this.userRoleInfoService = userRoleInfoService;
	}
	public List<RoleDto> getListRole() {
		return listRole;
	}
	public void setListRole(List<RoleDto> listRole) {
		this.listRole = listRole;
	}

	public String getFindAcc() {
		return findAcc;
	}

	public void setFindAcc(String findAcc) {
		this.findAcc = findAcc;
	}
	public String getFindName() {
		return findName;
	}
	public void setFindName(String findName) {
		this.findName = findName;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
}
